const HtmlWebpackPlugin = require('html-webpack-plugin');
const path = require('path');

module.exports = {
  // 入口
  entry: {
    index: './index.js',
    SpinePlugin: './modules/spine/SpinePluginDebug.js'
  },
  // 出口
  output: {
    filename: '[name].js',
    chunkFilename: '[name].js',
    path: path.resolve(__dirname, '../@localhost.express/public/phaser/dist'),
    clean: true,
    publicPath: './',
    assetModuleFilename: 'assets/[name].[contenthash:8].[ext]',
    environment: {
      arrowFunction: false
    }
  },
  // 代码分离
  optimization: {
    runtimeChunk: 'single',
    splitChunks: {
      cacheGroups: {
        commons: {
          //产生一个Chunk
          chunks: 'initial',
          minChunks: 2,
          maxInitialRequests: 5, // The default limit is too small to showcase the effect
          minSize: 0 // This is example is too small to create commons chunks
        },
        vendor: {
          //产生一个Chunk
          test: /node_modules/,
          chunks: 'initial',
          name: 'vendor',
          priority: 10,
          enforce: true
        }
      }
    }
  },
  // 插件
  plugins: [new HtmlWebpackPlugin({
    template: 'index.html',
    inject: 'body'
  })],
  // 模式
  mode: 'development',
  // loader
  module: {
    rules: [
      {
        test: /\.js$/,
        exclude: /(node_modules|bower_components)/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['@babel/preset-env']
          }
        }
      },
      {
        test: /\.(png|svg|jpg|jpeg|gif)$/i,
        type: 'asset/resource',
      },
      {
        test: /\.css$/i,
        use: ['style-loader', 'css-loader'],
      },
    ]
  }
};